/*
 *  $Id: Config.cs 1216 2010-11-26 17:35:07Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2010 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System;
using System.IO;

using Ragima.Languages;

using Nereal.Serialization;

namespace Ragima {
    /// <summary>
    /// Настройки игры
    /// </summary>
    [XmlRootName("config")]
    public sealed class Config {
        public const string ConfigFileName = "Config.xml";
        public const string DefaultConfigFileName = "DefaultConfig.xml";

        private static FilePath ConfigFullName {
            get { return Paths.User[ConfigFileName]; }
        }
        private static FilePath DefaultConfigFullName {
            get { return Paths.User[DefaultConfigFileName]; }
        }

        private Language _currentLanguage;

        [XmlName("lang")]
        public Language CurrentLanguage {
            get { return _currentLanguage; }
            set {
                SetCurrentLanguage(value ?? Language.AllLanguages.GetDefaultLanguage());
                ForceDefaultName();
            }
        }

        [XmlName("player1")]
        public readonly PlayerInfo Player1;
        [XmlName("player2")]
        public readonly PlayerInfo Player2;
        [XmlName("connection")]
        public readonly ConnectionConfig Connection;
        [XmlName("sound")]
        public readonly SoundConfig Sound;
        [XmlName("interface")]
        public readonly InterfaceConfig Interface;

        public Config() {
            SetCurrentLanguage(Application.AllLanguages.GetDefaultLanguage());
            Player1 = new PlayerInfo("player1");
            Player2 = new PlayerInfo("player2");
            Connection = new ConnectionConfig();
            Sound = new SoundConfig();
            Interface = new InterfaceConfig();
        }

        public static Config CreateDefaultConfig() {
            var config = new Config();
            config.ForceDefaultName();
            return config;
        }

        public static Config Load() {
            var config = CreateDefaultConfig();
            config.Load(DefaultConfigFullName);
            config.Load(ConfigFullName);
            config.ForceDefaultName();
            return config;
        }

        private void Load(string filename) {
            if (!File.Exists(filename))
                return;
            //try {
                Serializer.Default.Deserialize(this, filename);
            //} catch {}
        }

        public void Save() {
            Serializer.Default.Serialize(this, ConfigFullName);
        }

        private void SetCurrentLanguage(Language lang) {
            _currentLanguage = lang;
            Language.CurrentLanguage = lang;
        }

        private void ForceDefaultName() {
            Player1.ForceDefaultName(CurrentLanguage, Resources.Player.DefaultPlayer1);
            Player2.ForceDefaultName(CurrentLanguage, Resources.Player.DefaultPlayer2);
        }
    }

    public sealed class ConnectionConfig {
        public const string DefaultDirectIPAddress = "127.0.0.1";

        public ConnectionConfig() {
            DirectIPAddress = DefaultDirectIPAddress;
        }

        [XmlName("directIP")]
        public string DirectIPAddress { get; set; }
    }

    public sealed class SoundConfig {
        private const int DefaultVolume = 100;

        public SoundConfig() {
            DriverEnabled = false;
            MusicEnabled = true;
            SoundEnabled = true;
            MusicVolume = DefaultVolume;
            SoundVolume = DefaultVolume;
        }

        [NotSerialize]
        public bool DriverEnabled { get; set; }

        [XmlName("musicEnabled")]
        public bool MusicEnabled { get; set; }

        [XmlName("soundEnabled")]
        public bool SoundEnabled { get; set; }

        [XmlName("musicVolume")]
        public int MusicVolume { get; set; }

        [XmlName("soundVolume")]
        public int SoundVolume { get; set; }
    }

    public sealed class InterfaceConfig {
        public InterfaceConfig() {
            AutoMeditation = true;
            AutoTargetTab = true;
            DetailAffectDescription = true;
        }

        [XmlName("autoMeditation")]
        public bool AutoMeditation { get; set; }

        [XmlName("autoTargetTab")]
        public bool AutoTargetTab { get; set; }

        [XmlName("detailAffectDescription")]
        public bool DetailAffectDescription { get; set; }
    }
}
